/*
 * Javlov - a Java toolkit for reinforcement learning with multi-agent support.
 * 
 * Copyright (c) 2009 Matthijs Snel
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.javlov.world;

import java.awt.Shape;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.javlov.Action;
import net.javlov.Agent;
import net.javlov.State;
import net.javlov.VectorState;

public class BasicAgentBody extends BasicBody implements AgentBody {

	protected List<Sensor<double[]>> sensors;
	protected List<PhysicalSensor> physSensors;

	public BasicAgentBody() {
		this(null);
	}
	
	public BasicAgentBody(Shape figure) {
		super(figure);
		sensors = new ArrayList<Sensor<double[]>>();
		physSensors = new ArrayList<PhysicalSensor>();
	}
	
	public void add(Sensor s) {
		sensors.add(s);
		if ( s instanceof PhysicalSensor )
			physSensors.add((PhysicalSensor) s);
	}
	
	@SuppressWarnings("unchecked")
	public List<Sensor<double[]>> getSensors() {
		return Collections.unmodifiableList(sensors);
	}

	@Override
	public void setLocation(double x, double y) {
		for ( PhysicalSensor sensor : physSensors )
			sensor.translate(x - location.getX(), y - location.getY());
		super.setLocation(x,y);
	}
	
	//// ENVIRONMENT METHODS //////
	@Override
	public double executeAction(Action act, Agent a) {
		throw new UnsupportedOperationException();
	}

	@Override
	public State<double[]> getObservation(Agent agent) {
		VectorState st = new VectorState();
		for ( Sensor<double[]> s : sensors )
			st.append(s.getReading());
		return st;
	}

	@Override
	public void init() {
		for ( Sensor s : sensors )
			s.init();
	}
	
	@Override
	public void reset() {
		for ( Sensor s : sensors )
			s.reset();
	}

	@Override
	public int getObservationDim() {
		int dim = 0;
		for ( Sensor<double[]> s : sensors )
			dim += s.getReadingDim();
		return dim;
	}
}
